# --- building ---
FROM harbor.xhhk/common/pyinstaller:py37-alpine AS builder

ARG APP_NAME="a637"
ARG APP_FILE="main.py"
ARG WAITING_SCRIPTS="#!bin/sh"

ADD requirements.txt /data/
RUN pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com
RUN pip3 --no-cache-dir install -i  https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com -r /data/requirements.txt
RUN mkdir -p /app/${APP_NAME}
WORKDIR /app/${APP_NAME}

COPY . ./

RUN pyinstaller --name=${APP_NAME} --clean --key $(openssl rand -base64 32) $APP_FILE \
   --hidden-import=apscheduler.triggers.interval \
   --hidden-import=apscheduler.triggers.cron


RUN cp -R ./etc/ dist/${APP_NAME}



# --- producting ---
FROM harbor.xhhk/common/workerimage:alpine

ARG APP_NAME="a637"
ENV APP_NAME=${APP_NAME}

RUN mkdir -p /app/${APP_NAME}
WORKDIR /app/${APP_NAME}
COPY --from=builder /app/${APP_NAME}/dist/${APP_NAME} ./
RUN chmod +x /app/${APP_NAME}/${APP_NAME}
RUN mkdir -p /app/${APP_NAME}/log


RUN echo -e "${WAITING_SCRIPTS}\n$@\n./${APP_NAME}\n" > /usr/local/bin/entrypoint.sh \
    && chmod +x /usr/local/bin/entrypoint.sh

RUN chown 2020:2020 -R /app/${APP_NAME} && chown 2020:2020 /usr/local/bin/entrypoint.sh
USER 2020
WORKDIR /app/${APP_NAME}

ENTRYPOINT [ "sh", "/usr/local/bin/entrypoint.sh" ]
